package com.zkthink.zhl.order.service.impl;


import cn.hutool.core.bean.BeanUtil;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.zkthink.base.R;
import com.zkthink.base.service.SuperServiceImpl;
import com.zkthink.ceres.member.entity.Member;
import com.zkthink.ceres.member.service.MemberService;
import com.zkthink.clh.order.dto.ChlConfigSaveDTO;
import com.zkthink.clh.order.dto.CouponsPageDTO;
import com.zkthink.clh.order.dto.CouponsSaveDTO;
import com.zkthink.clh.order.dto.CouponsUpdateDTO;
import com.zkthink.clh.order.entity.AppletVip;
import com.zkthink.clh.order.entity.ChlConfig;
import com.zkthink.clh.order.entity.ChlCoupons;
import com.zkthink.clh.order.entity.ChlOrder;
import com.zkthink.context.BaseContextHandler;
import com.zkthink.zhl.order.dao.ChlConfigMapper;
import com.zkthink.zhl.order.dao.ChlCouponsMapper;
import com.zkthink.zhl.order.service.ChlCouponsService;
import lombok.extern.slf4j.Slf4j;
import org.checkerframework.checker.units.qual.C;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.math.BigDecimal;
import java.time.LocalDateTime;
import java.util.List;

/**
 * <p>
 * 业务实现类
 * 车龙惠消费金表
 * </p>
 *
 * @author chenke
 * @date 2022-04-26
 */
@Slf4j
@Service
public class ChlCouponsServiceImpl extends SuperServiceImpl<ChlCouponsMapper, ChlCoupons> implements ChlCouponsService {
    @Autowired
    private ChlConfigMapper chlConfigMapper;

    @Autowired
    private MemberService memberService;

    @Autowired
    private ChlCouponsService chlCouponsService;

    /**
     * 添加 消费金
     *
     * @param dto
     * @return
     */
    @Override
    public R addCoupons(CouponsSaveDTO dto) {
        ChlCoupons chlCoupons = baseMapper.selectByRecharge(dto.getRecharge());
        if (chlCoupons != null) {
            return R.fail("不能重复添加");
        }
        ChlCoupons chlCoupons1 = new ChlCoupons();
        BeanUtil.copyProperties(dto, chlCoupons1);
        baseMapper.insert(chlCoupons1);
        return R.success();
    }

    /**
     * 修改 消费金
     *
     * @param dto
     * @return
     */
    @Override
    public R editCoupons(CouponsUpdateDTO dto) {
        ChlCoupons chlCoupons = baseMapper.selectByRecharge(dto.getRecharge());
        if (chlCoupons != null) {
            return R.fail("金额不可重复");
        }
        ChlCoupons chlCoupons1 = new ChlCoupons();
        BeanUtil.copyProperties(dto, chlCoupons1);
        baseMapper.updateById(chlCoupons1);
        return R.success();
    }

    @Override
    public IPage<ChlCoupons> findCoupons(IPage page, CouponsPageDTO model) {
        return baseMapper.findCoupons(page, model);
    }

    @Override
    public List<AppletVip> selectVip() {
        return baseMapper.selectVip();
    }

    /**
     * 根据充值金额查询 消费金
     *
     * @param recharge
     * @return
     */
    @Override
    public ChlCoupons findRecharge(BigDecimal recharge) {
        return baseMapper.findRecharge(recharge);
    }

    @Override
    public R delectId(Long id) {
        return R.success(baseMapper.updateDelect(id));
    }

    /**
     * app获取消费金
     *
     * @return
     */
    @Override
    public List<ChlCoupons> appCoupons() {
        return baseMapper.selectAll();
    }

    @Override
    public BigDecimal getVip() {
        ChlConfig byparameterUnique = chlConfigMapper.findByparameterUnique("chl.vip");
        return byparameterUnique.getParameterValue();
    }

    /**
     * 购买vip
     *
     * @param vipId
     * @return
     */
    @Override
    public R buyVip(String vipId) {
        ChlOrder chlOrder = new ChlOrder();
        AppletVip appletVip = chlConfigMapper.findVipById(vipId);
        if (appletVip == null) {
            return R.fail("vip套餐不存在");
        }
        //支付后回调参数逻辑，支付申请未完成，暂时放在购买里
        int vipDay = Integer.parseInt(appletVip.getPackageTime());
        Long mid = BaseContextHandler.getUserId();
        Member member = memberService.getById(mid);
        if (member.getVipTime() != null) {
            if (member.getVipTime().isAfter(LocalDateTime.now())) {
                member.setVipTime(member.getVipTime().plusDays(vipDay));
            } else {
                member.setVipTime(LocalDateTime.now().plusDays(vipDay));
            }
        } else {
            member.setVipTime(LocalDateTime.now().plusDays(vipDay));
        }
        member.setLevel(1);
        memberService.updateAllById(member);
//        //判断用户是否是VIP
//        if (member.getVipTime().isAfter(LocalDateTime.now())){
//            ChlCoupons chlCoupons = chlCouponsService.findRecharge(recharge);
//            if (chlCoupons != null) {
//                member.setCoupons(chlCoupons.getCoupons());
//            }
//            member.setBalance(recharge);
//            memberService.updateAllById(member);
//        }
        return R.success();
    }

    @Override
    public R buyChlCoupons(String chlCouponsId) {
        ChlCoupons chlCoupons = baseMapper.selectById(chlCouponsId);
        if (chlCoupons == null) {
            return R.fail("消费金套餐不存在");
        }
        //支付后回调参数逻辑，支付申请未完成，暂时放在购买里
        Long mid = BaseContextHandler.getUserId();
        Member member = memberService.getById(mid);
        member.setCoupons(member.getCoupons().add(chlCoupons.getCoupons()));
        memberService.updateAllById(member);
        return  R.success();
    }

    /**
     * 获取店铺入住费
     *
     * @return
     */
    @Override
    public BigDecimal getRuzhufei() {
        ChlConfig byparameterUnique = chlConfigMapper.findByparameterUnique("db.rz");
        return byparameterUnique.getParameterValue();
    }
}
